package com.hdaccp.xingmu.controller;

import com.hdaccp.xingmu.dao.*;
import com.hdaccp.xingmu.entity.Costitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/costitem")
public class ConsitemCon {

    @Autowired
    private CostitemDao consitemDao;
    @Autowired
    private SalesrecordCon salesrecordCon;
    @Autowired
    private house house;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private PayDao payDao;
    @Autowired
    private userdetailsDao userdetailsDao;
	
    @Autowired
    private EntityManager entityManager;
	

    @RequestMapping("/tiao")
    public String tiao(){

        return "WEB-INF/jsp/scwuyefei";
    }
    @RequestMapping("/tiao2")
    public String tiao2(){

        return "WEB-INF/jsp/wyfcx";
    }
    @RequestMapping("/tiao3")
    public String tiao3(){

        return "WEB-INF/jsp/sqwyf";
    }

    @RequestMapping("/shu")
    @ResponseBody
    public List<Costitem> list(){
        return consitemDao.findAll();
    }



    @RequestMapping("/c")
    public String All(){
        return "WEB-INF/jsp/feixiangshezhi";
    }

    @RequestMapping("/sj")
    @ResponseBody
    public Map<String,Object> query(int pageSize,int pageNumber){
        //分页
        String jpal = "from Costitem where 1=1";

        Query qu = entityManager.createQuery(jpal);
        //起始页书
        qu.setFirstResult((pageNumber - 1) * pageSize);
        //结束页数
        qu.setMaxResults(pageSize);
        //查询多少条数据
        String jpa = "select count(c) from Costitem c";

        Query q = entityManager.createQuery(jpa);
        Long count = (Long) q.getSingleResult();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("total", count);
        map.put("rows",qu.getResultList());
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(Costitem cos)throws UnsupportedEncodingException {
        consitemDao.save(cos);
        return query(5,1);
    }
    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(Long id){
        consitemDao.deleteById(id);
     /*   costitemDao.findAll();*/
        return query(5,1) ;
    }
    @RequestMapping("/upd")
    @ResponseBody
    public Map<String,Object> upd(Costitem cos) throws UnsupportedEncodingException{
        consitemDao.save(cos);
        return query(5,1);
    }

}
